package com.example.starseas_phone;

import com.example.starseas_phone.Utils.Utils_Schedule;
import com.example.starseas_phone.Utils.MySubject;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据类，加载课程数据
 * @author zf
 *
 */
public class SubjectRepertory {

    public static List<Integer> getWeekList(String weeksString){
        List<Integer> weekList=new ArrayList<>();
        if(weeksString==null||weeksString.length()==0) return weekList;

        weeksString=weeksString.replaceAll("[^\\d\\-\\,]", "");
        if(weeksString.indexOf(",")!=-1){
            String[] arr=weeksString.split(",");
            for(int i=0;i<arr.length;i++){
                weekList.addAll(getWeekList2(arr[i]));
            }
        }else{
            weekList.addAll(getWeekList2(weeksString));
        }
        return weekList;
    }

    public static List<Integer> getWeekList2(String weeksString){
        List<Integer> weekList=new ArrayList<>();
        int first=-1,end=-1,index=-1;
        if((index=weeksString.indexOf("-"))!=-1){
            first=Integer.parseInt(weeksString.substring(0,index));
            end=Integer.parseInt(weeksString.substring(index+1));
        }else{
            first=Integer.parseInt(weeksString);
            end=first;
        }
        for(int i=first;i<=end;i++)
            weekList.add(i);
        return weekList;
    }

    public static List<MySubject> parses(List<Utils_Schedule> list){
        List<MySubject> courses = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            Utils_Schedule utils_schedule = list.get(i);
            String term = utils_schedule.getSchool_Semester();
            String name = utils_schedule.getCourse();
            String room = utils_schedule.getSchedule_Placel()+"";
            String teacher = utils_schedule.getWorkerId()+"";
            List<Integer> weekList = getWeekList(utils_schedule.getSchedule_Weekly());
            int start = Integer.parseInt(utils_schedule.getSchedule_What())*2-1;
            int step = 2;
            int day = Integer.parseInt(utils_schedule.getSchedule_Week());
            courses.add(new MySubject(term,name, room, teacher, weekList, start, step, day, -1,null));
        }
        return courses;
    }

}
